﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vngoodlife.Singleton;

namespace vngoodlife.Models
{
    public class ArticleSubjectModels
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="newSubjectName"></param>
        /// <param name="memberName"></param>
        /// <returns></returns>
        internal bool InsertSubject(string newSubjectName, string memberName)
        {
            try
            {
                ArticleSubject sub = new ArticleSubject { ArtSubjectName = newSubjectName.Trim(), MemberName = memberName };
                sub.CreateDate = DateTime.Now;
                sub.UpdateDate = DateTime.Now;
                DataSingleton.Instance.GetLinQContext().ArticleSubjects.InsertOnSubmit(sub);
                DataSingleton.Instance.GetLinQContext().ArticleSubjects.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }


        /// <summary>
        /// Get Article Subject Details
        /// </summary>
        /// <param name="articleSubjectID"></param>
        /// <returns></returns>
        internal ArticleSubject GetArticleSubjectDetails(int articleSubjectID)
        {
            return DataSingleton.Instance.GetLinQContext().ArticleSubjects.SingleOrDefault(a => a.ArtSubjectID == articleSubjectID);
        }


        /// <summary>
        /// Get Article Subject List 
        /// </summary>
        /// <returns></returns>
        internal IQueryable<ArticleSubject> GetArticleListBackEnd()
        {
            return DataSingleton.Instance.GetLinQContext().ArticleSubjects.OrderByDescending(a => a.ArtSubjectID);
        }

        /// <summary>
        /// Update Article Subject
        /// </summary>
        /// <param name="artSub"></param>
        /// <param name="memberName"></param>
        internal void EditArticleSubject(ArticleSubject artSub, string adminName)
        {
            try
            {
                ArticleSubject temArtSub = DataSingleton.Instance.GetLinQContext().ArticleSubjects.SingleOrDefault(a => a.ArtSubjectID == artSub.ArtSubjectID);
                temArtSub.ArtSubjectName = artSub.ArtSubjectName.Trim();
                temArtSub.AdminName = adminName;
                temArtSub.UpdateDate = DateTime.Now;

                DataSingleton.Instance.GetLinQContext().ArticleSubjects.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
            }
        }


        /// <summary>
        /// Delete Article Subject and all article of this article subject.
        /// </summary>
        /// <param name="articleSubjectID"></param>
        internal bool DeleteArticleSubject(int articleSubjectID)
        {
            try
            {
                ArticleSubject temArtSub = DataSingleton.Instance.GetLinQContext().ArticleSubjects.SingleOrDefault(a => a.ArtSubjectID == articleSubjectID);
                DataSingleton.Instance.GetLinQContext().ArticleSubjects.DeleteOnSubmit(temArtSub);
                DataSingleton.Instance.GetLinQContext().ArticleSubjects.Context.SubmitChanges();
            }
            catch (Exception)
            {
                DataSingleton.Instance.RefreshInstance();
                return false;
            }
            return true;
        }


        /// <summary>
        /// Get all articles of an article subject
        /// </summary>
        /// <param name="artSubjectID"></param>
        /// <returns></returns>
        internal IQueryable<Article> GetArticlesOfArticleSubject(int artSubjectID)
        {
            return DataSingleton.Instance.GetLinQContext().Articles.Where(a => a.ArtSubjectID == artSubjectID);
        }



        /// <summary>
        /// Get ArticleSubjectID based on Subject Seourl. If seourl is not exist, it will return zero.
        /// </summary>
        /// <param name="subjectSeoUrl"></param>
        internal ArticleSubject GetArticleSubjectBySubjectSeoUrl(string subjectSeoUrl)
        {
            try
            {
                return DataSingleton.Instance.GetLinQContext().ArticleSubjects.SingleOrDefault(u => u.ArtSubjectSeoUrl == subjectSeoUrl);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}